#include "ports.h"

void initGPIO(void)
{
	drive_go = 0;
	readValue = 0;
	show_bin = 0;

	// set LED PORT D pins as outputs
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, (PD_0 | PD_1 | PD_2 | PD_3 | PD_4 | PD_5));
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, (PA_5 | PA_6 | PA_7));
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, (PB_3 | PB_4));
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, redled|bluled|grnled);

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, (PA_2 | PA_3 | PA_4));
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, (PD_6 | PD_7));

	// set Port A 2,3,4 and Port B 6,7 as inputs
	//ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, (PA_2 | PA_3 | PA_4));
	//ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, (PD_6 | PD_7));

	//onboard button interrupts
	ROM_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, (btn1|btn2));
	ROM_GPIOPadConfigSet(GPIO_PORTF_BASE, (btn1|btn2), GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
	ROM_GPIOIntTypeSet(GPIO_PORTF_BASE, (btn1|btn2), GPIO_FALLING_EDGE);

	//port b rampdown interrupt
	ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, _rampdown);
	ROM_GPIOPadConfigSet(GPIO_PORTB_BASE, _rampdown, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
	ROM_GPIOIntTypeSet(GPIO_PORTB_BASE, _rampdown, GPIO_FALLING_EDGE);

	ROM_GPIOPinIntClear(GPIO_PORTF_BASE, (btn1|btn2));
	ROM_GPIOPinIntClear(GPIO_PORTB_BASE, _rampdown);

	ROM_IntEnable(INT_GPIOF);
	ROM_IntEnable(INT_GPIOB);
	//ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, PF_0);

	//set Laser ports as inputs
	ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, (PB_0 | PB_1));

	// set Port B and E stepper pins as output
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, (PB_5));
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, (PE_5 | PE_1));
	ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, PE_4);

	//set belt drive to brake
	ROM_GPIOPinWrite(GPIO_PORTA_BASE, (IA|EA), 0);
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, (IB|EB), 0);

	ROM_GPIOPinIntEnable(GPIO_PORTF_BASE, (btn1|btn2));
	ROM_GPIOPinIntEnable(GPIO_PORTB_BASE, _rampdown);

}

void PortAIntHandler(void)
{
	//moved interrupt clear to end to prevent bouncing since it's too fast
	//ROM_GPIOPinIntClear(GPIO_PORTA_BASE, PA_2);

	//--ROM_GPIOPinWrite(GPIO_PORTF_BASE, redled, redled);

	//re-init readValue
	//readValue = 0;

	//--ROM_SysCtlDelay(2000000);
	//--ROM_GPIOPinIntClear(GPIO_PORTA_BASE, PA_2);		//clear interrupt here to prevent softbouce
	//--ROM_GPIOPinWrite(GPIO_PORTF_BASE, redled, redled);
}

void PortFIntHandler(void)
{
	unsigned int isr = GPIOPinIntStatus(GPIO_PORTF_BASE, true);
	//ROM_GPIOPinIntClear(GPIO_PORTF_BASE, (btn1|btn2));

	//bt_UARTsend("\033[2J");		//clear screen
	//bt_UARTsend("Interrupt...Port F\r\n");
	if(isr & btn2)
	{
		drive_go ^= 1;
		if(drive_go)
		{
			belt_driver(1);
			ROM_GPIOPinWrite(GPIO_PORTF_BASE, bluled, ~bluled);
		}
		else
		{
			belt_driver(0);
			print_bin_number();
			turnTimes();
			//printllQueue(&head,&tail);
			if(speak_on) SYN_Say((unsigned char *)"[v11] soundy 系统暂停");
			ROM_GPIOPinWrite(GPIO_PORTF_BASE, bluled, bluled);
		}
	}

	if(isr & btn1)
	{
		//
		bt_UARTsend("button 1\r\n");
	}

	ROM_GPIOPinIntClear(GPIO_PORTF_BASE, (btn1|btn2));
}

void PortBIntHandler(void)
{
	if(rampdown_condition == 0)
		rampdown_condition = 1;
	else if(rampdown_condition == 1)
		rampdown_condition = 0;

	if(rampdown_condition == 1)
	{
		bt_UARTsend("\r\nPreparing to ramp down\r\n");
		if(speak_on) SYN_Say((unsigned char *)Volume"msgd 停机，请"); //system stop, please
	}
	else if(rampdown_condition == 0)
	{
		bt_UARTsend("\r\nCanceling ramp down\r\n");
		g_ramp_counter=rampdown_time_common;
		if(speak_on) SYN_Say((unsigned char *)Volume"msgc 停止，取消"); //stop, cancel
	}

	ROM_GPIOPinIntClear(GPIO_PORTB_BASE, _rampdown);
}
